package figure8.GUI;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import figure8.Controller;
import figure8.LoopTiming;
import figure8.events.PlaybackEvent;
import figure8.events.PlaybackListener;

/**
 * This is a very simple panel for displaying the timing information of a
 * project's loop.
 */
public class TimingPanel extends JPanel implements PlaybackListener {
	private static final long serialVersionUID = -4477491430477279526L;
	private JProgressBar progress = new JProgressBar(0, 100);
	private boolean start = false;
	private boolean reset = false;
	private boolean stopBar = false;
	private double length;
	private int tempo, progressLength;
	private int beats;

	ProgressThread update = null;

	public TimingPanel(Controller controller, LoopTiming timing) {
		super();

		controller.addPlaybackListener(this);

		// Use a grid layout of 1 row and 5 columns so that the labels are
		// equally spaced along the panel.
		this.setLayout(new GridLayout(2, 1));
		JPanel timings = new JPanel(new GridLayout(1, 4));
		tempo = timing.getTempo();
		// Add the timing labels to the panel, with the text centered
		// horizontally.
		length = timing.getLength();
		progressLength = timing.getBeats() * timing.getBars() * 20;
		progress.setMaximum(progressLength);
		beats = timing.getBeats();
		progress.setStringPainted(true);
		progress.setString("");
		timings.add(new JLabel("Tempo: " + timing.getTempo() + "bpm",
				JLabel.HORIZONTAL));
		timings.add(new JLabel("Bars per loop: " + timing.getBars(),
				JLabel.HORIZONTAL));
		timings.add(new JLabel("Time Signature: " + timing.getBeats() + "/"
				+ timing.getNote(), JLabel.HORIZONTAL));
		timings.add(new JLabel("Length: " + timing.getLength() + "s",
				JLabel.HORIZONTAL));
		this.add(timings);
		this.add(progress);
		progress.setValue(-1);

		// Set the preferred size (minimum) of each label so the window cannot
		// be resized so small that they are not visible.
		for (Component button : getComponents()) {
			button.setPreferredSize(new Dimension(800, 20));
		}

	}
	
	public void startBar() {
		this.update = new ProgressThread();
		stopBar = false;
		this.update.start();
	}

	class ProgressThread extends Thread {
		public void run() {
			int increments = 1;
			double beatTime;
			while (stopBar == false) {
				if (start == true) {
					// increments = increments % beats;
					if (increments > beats * 20) {
						increments = 1;
					}
					if (increments % 20 == 0 || increments == 1) {
						progress.setString(Integer
								.toString((increments / 20) + 1)
								+ " / "
								+ Integer.toString(beats));
					}
					beatTime = 60 / (double) tempo;
					progress.setValue(progress.getValue() + 1);
					if (progress.getValue() == progressLength)
						progress.setValue(0);
					try {
						Thread.sleep((int) (50 * beatTime));
					} catch (Exception e) {

					}
					increments++;
				}
				if (reset == true) {
					reset = false;
					progress.setValue(0);
					progress.setString("");
					increments = 1;
				}
			}
		}

	}

	@Override
	public void playbackChanged(PlaybackEvent event) {
		switch (event.getState()) {
		case PLAYING:
			start = true;
			break;
		case STOPPED:
			start = false;
			stopBar = true;
			progress.setValue(0);
			progress.setString("");
			break;
		case RESET:
			reset = true;
			break;
		}
	}
}
